package com.aisino.mplearning;

import com.aisino.mplearning.utils.SqlMetaUtils;

import java.sql.*;

/**
 * @author guochuantao
 * @version 1.0
 * @description
 * @since 2024/9/6 下午4:58
 */
public class ColumnTypeChecker {
    public static void main(String[] args) {
        String url = "jdbc:mysql://172.19.12.222:3306/dyw_test01?useUnicode=true&characterEncoding=utf8&nullCatalogMeansCurrent=true"; // 替换为你的数据库URL
        String user = "root"; // 替换为你的数据库用户名
        String password = "sjzl@2024"; // 替换为你的数据库密码
        String tableName = "xxl_job_log"; // 替换为你想要查询的表名

        try (Connection conn = DriverManager.getConnection(url, user, password)) {
            DatabaseMetaData metaData = conn.getMetaData();
            ResultSet columns = metaData.getColumns(null, null, tableName, null);

            ResultSet rs = conn.createStatement().executeQuery("SELECT TABLE_NAME,COLUMN_NAME,DATA_TYPE,IS_NULLABLE,COLUMN_DEFAULT," +
                    "COLUMN_TYPE,COLUMN_KEY,COLUMN_COMMENT,NUMERIC_PRECISION,NUMERIC_SCALE " +
                    "FROM INFORMATION_SCHEMA.COLUMNS " +
                    "WHERE TABLE_SCHEMA = 'dyw_test01' AND TABLE_NAME = 'xxl_job_log';");
            System.out.println("columns = " + columns);
            while (columns.next() && rs.next()) {
                String columnName = columns.getString("COLUMN_NAME");
                int columnType = columns.getInt("DATA_TYPE");
                // 输出列名和类型
                System.out.println("Column Name: " + columnName + ", =====>SQL Type: " + columnType);
/*            }

            ResultSet rs = conn.createStatement().executeQuery("SELECT TABLE_NAME,COLUMN_NAME,DATA_TYPE,IS_NULLABLE,COLUMN_DEFAULT," +
                    "COLUMN_TYPE,COLUMN_KEY,COLUMN_COMMENT,NUMERIC_PRECISION,NUMERIC_SCALE " +
                    "FROM INFORMATION_SCHEMA.COLUMNS " +
                    "WHERE TABLE_SCHEMA = 'dyw_test01' AND TABLE_NAME = 'xxl_job_log';");
            while (rs.next()) {*/
                String name = rs.getString("COLUMN_NAME");
                System.out.print("name = " + name + "\t");
                String type = rs.getString("DATA_TYPE");
                System.out.print("type = " + type + "\t");
                String type1 = rs.getString("COLUMN_TYPE");
                System.out.print("type1 = " + type1 + "\t");
                int numericScare = rs.getInt("NUMERIC_SCALE");
                System.out.print("numericScare = " + numericScare);
                System.out.println();
                int dbLengthByDataType = SqlMetaUtils.getDbLengthByDataType(type1);
                System.out.println("===长度====>" + dbLengthByDataType);
                System.out.println();
            }

        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}